//admin.js
//上传
function swfupload(param){
    var opt = param || {};
    var querystr = $.param(opt); querystr = querystr ? "&"+querystr : '';
    var url = "/?m=admin&c=Attachment&a=index"+querystr;
    var upload = $.dialog.open(
        url,
        {
            id: 'swfupload',
            title: '',
            lock: 'true',
            window: 'top',
            width: 600,
            padding:0,
            init: function(){
                var iframeWin = this.iframe.contentWindow;
                if (opt.typeNo==11) {
                    //图片
                    iframeWin.$('.ok').click(function(){
                        $num = iframeWin.$('#filelist > li.selected').length;
                        if ($num>0) {
                            $img=iframeWin.$('#filelist > li.selected').eq(0).find('img').attr('data');
                            $('#'+opt.id).html('<div class="picitem"><img src="/'+$img+'" /><input type="'+opt.input_type+'"  class="input" name="'+opt.input_name+'" value="'+$img+'"><a href="javascript:;" data="'+$img+'" onclick="pic_close(this)">X</a></div>');
                            upload.close();
                        }else{
                            alert('请上传文件或选择文件!');
                        };
                    });
                }

                if (opt.typeNo==10) {
                    //列表
                    iframeWin.$('.ok').click(function(){
                        $num = iframeWin.$('#filelist > li.selected').length;
                        if ($num>0) {
                            iframeWin.$('#filelist > li.selected').each(function(i){
                                $img=$(this).find('img').attr('data');
                                $img_name=$(this).find('img').attr('name');
                                $('#'+opt.id).append('<div class="picitem selected"><img src="/'+$img+'" title="'+$img_name+'"/><input type="'+opt.input_type+'" class="input" name="'+opt.input_name+'['+$img+']" value="描述"><a href="javascript:;" data="'+$img+'" onclick="pic_close(this)">X</a></div>');
                            });
                            upload.close();
                        }else{
                            alert('请上传文件或选择文件!');
                        };
                    });
                }

                if (opt.typeNo==21) {
                    //文件
                    iframeWin.$('.ok').click(function(){
                        $num = iframeWin.$('#filelist > li.selected').length;
                        if ($num>0) {
                            var file=iframeWin.$('#filelist > li.selected').eq(0);
                            $file=file.find('span').attr('data');
                            $file_name=file.find('span').attr('name');
                            $file_ext=file.find('span').attr('ext');
                            $file_size=file.find('span').attr('size');
                            $('#'+opt.id).html('<div class="fileitem selected"><span>文件名：<input type="'+opt.input_type+'" class="input" name="'+opt.input_name+'['+$file+']" value="'+$file_name+'" placeholder="名称"></span><a href="javascript:;" data="'+$file+'" onclick="pic_close(this)">X</a></div>');
                            upload.close();
                        }else{
                            alert('请上传文件或选择文件!');
                        };
                    });
                }

                if (opt.typeNo==20) {
                    //文件列表
                    iframeWin.$('.ok').click(function(){
                        $num = iframeWin.$('#filelist > li.selected').length;
                        if ($num>0) {
                            iframeWin.$('#filelist > li.selected').each(function(i){
                                $file=$(this).find('span').attr('data');
                                $file_name=$(this).find('span').attr('name');
                                $('#'+opt.id).append('<div class="fileitem selected"><span>文件名：<input type="'+opt.input_type+'" class="input" name="'+opt.input_name+'['+$file+']" value="'+$file_name+'" placeholder="名称"></span><a href="javascript:;" data="'+$file+'" onclick="pic_close(this)">X</a></div>');
                            });
                            upload.close();
                        }else{
                            alert('请上传文件或选择文件!');
                        };
                    });
                }

                iframeWin.$('.close').click(function(){
                    $num = iframeWin.$('#filelist > li.selected').length;
                    upload.close();
                });

            }
        }
    );
}

//移除列表节点
function pic_close(id){
    $file = $(id).attr('data');
    $.get('/?m=admin&c=Attachment&a=delfile&file='+$file, function(data){
        if(data!=''){
            $(id).parent().remove();
        } else {
            alert(data.info,'alert-error');
        }
    }, "json");

}

//添加拖动
function sortable(id){
    $(id).sortable();
}

function check_all(id){
    $data=$(id).attr('data');
    $checked = $(id).attr("checked");
    if ($checked) {
        $('input[data="'+$data+'_sub"]').attr('checked',true);
    }else{
        $('input[data="'+$data+'_sub"]').attr('checked',false);
    };

}

(function ($) {
    $(function(){

        $('.sort_btn').blur(function(){
            var _this = $(this);
            var href = _this.attr('data-href');
            var sort = _this.val();
            $.post(href,{sort:sort},function(data){
                if(data.err==0){
                    $.notice({'id':'dialog_notice','content':"<div class='dialog_con_notice'>"+data.msg+"</div>",'time':2});
                }else{
                    $.notice({'id':'dialog_notice','content':"<div class='dialog_con_notice err'>"+data.msg+"</div>",'time':2});
                }
            })
        })
        $('.status_btn').click(function(){
            _this = $(this);
            title = _this.attr('title');
            href = _this.attr('data-href');
            $.get(href,function(data){
                if(data.err=='0'){
                    if(data.sign=='add'){
                        _this.addClass('on');
                    }else{
                        _this.removeClass('on');
                    }
                    $.notice({'id':'dialog_notice','content':"<div class='dialog_con_notice'>"+data.msg+"</div>",'time':2});
                }else{
                    $.notice({'id':'dialog_notice','content':"<div class='dialog_con_notice err'>"+data.msg+"</div>",'time':2});
                }
            })
        })


        //ajax提交表单
        $('.ajax-form').submit(function(){
            var form=$(this);
            form.ajaxSubmit(function(data){
                if(data.err==1){
                    $.slide({'id':'dialog_notice','content':"<div class='dialog_center_notice'>"+data.msg+"</div>",'time':2,'fixed':true,'lock':true});
                    form.find('[name="info['+data.key+']"]').trigger('focus');
                }else if(data.err==2){
                    $.slide({'id':'dialog_notice','content':"<div class='dialog_center_notice'>"+data.msg+"</div>",'time':2,'fixed':true,'lock':true});
                }else if(data.err==0){
                    $.slide({'id':'dialog_notice','content':"<div class='dialog_center_notice'>"+data.msg+"</div>",'time':2,'fixed':true,'lock':true,'close':function(){
                        if(data.action=='reload'){
                            window.location.reload();
                        }else if(data.action=='redirect'){
                            window.location.href=data.url;
                        }else if(data.action=='login'){
                            window.location.reload();
                        }
                    }});
                }else{
                    $.slide({'id':'dialog_notice','content':"<div class='dialog_center_notice'>"+data.msg+"</div>",'time':2,'fixed':true,'lock':true});
                }
            });
            return false;
        });
    })
})(jQuery);